/**
*  @author: yaoyuan
*  @version: 1.1
*  Description: web请求的拦截器				
*  created: 2014.09.23
*  updated: 2014.09.23
*  Update Logs:
*  1. 创建对象
*  2. ES6羽化
**/

/**
 * Module exports.
 */

export default class Interceptor {
	/**
	 * Interceptor constructor method
	 */
	constructor(req) {
		this.req = req;
		this.query = this.req.query;
		this.username = this.req.session.username;
		this.userid = this.req.session.userid;
	}
	/**
	 * 查看路由地址, 查看有无不需过虑的地址
	 * @param  请求地址
	 * @returns {Boolean}
	 */
	NoFilterUrl() {
		// 全局无需验证的根目录地址
		const globalUrl = '^/(pub|login|loginCheck)/.*&';
		// ture无需验证next, false需用户验证
		const is = false;
		const urlArray = ['^/vote/json/view/\\w{24}$',
		                '^/vote/json/result/\\w{24}$',
		                '^/vote/view/\\w{24}$',
		                '^/vote/result/\\w{24}$',
		                '^/lottery/view/\\w{24}$'];

		if(new RegExp(globalUrl).test(this.originalUrl)) return true;

		urlArray.forEach((url) => {
			if(new RegExp(url).test(this.originalUrl)) {
				is = true;
				return ;
			}
		})
		return is;	
	}
	/**
	 * 验证服务是否有session保存,现在只是对用户名和唯一标识做验证
	 * @param session session对象
	 * @returns {Boolean}
	 */
	VerificationUser() {
		return !!(this.username&&this.userid);
	}

	/**
	 * 标记伪码, 对错误的url地址统一处理
	 * @param code 字符串伪码值
	 * @returns {json}
	 */
	PseudoCode(code) {
		// 默认属性
		var json = {is: true};
		switch (code) {
		case 'login': json = {is:false, redirect:'/login?'+this.query};
			break;
		default:
			break;
		}
		return json;
	}
}



